package com.liner.homework.third;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
    public static void main(String[] args) {
        //1. Collection集合基本方法的练习：add()、size()、isEmpty()、clear()、contains()、remove()
        Collection<Integer> c1 = new ArrayList<>();
        c1.add(1);
        c1.add(4);
        c1.add(6);
        c1.add(8);
        c1.add(2);
        System.out.println(c1);
        System.out.println("集合的长度是:" + c1.size());
        System.out.println("集合是否为空:" + c1.isEmpty());
        System.out.println("集合中是否包含 2:" + c1.contains(2));
        System.out.println("删除集合中的 8:" + c1.contains(8));
        System.out.println(c1);
        System.out.println("将集合清空:");
        c1.clear();
        System.out.println("集合是否为空:" + c1.isEmpty());

        //2. Collection集合间操作的练习：addAll()、containsAll()、retainAll()、removeAll()
        ArrayList<Integer> c2 = new ArrayList<>();
        c2.add(312);
        c2.add(122);
        c2.add(217);
        c2.add(387);
        c2.add(982);
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
        System.out.println("将集合c2添加给c1:" + c1.addAll(c2));
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
        c1.add(1254);

        System.out.println("给c1添加些元素c1包含c2吗:" + c1.containsAll(c2));
        System.out.println("取出c1中与c2的交集:");
        c1.retainAll(c2);
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);
        System.out.println("将c2和c1的交集找出并删除且保留c2:");
        c1.removeAll(c2);
        System.out.println("c1:" + c1);
        System.out.println("c2:" + c2);


    }
}
